package com.library.service.impl;

import com.library.entity.Book;
import com.library.entity.Borrow;
import com.library.repository.AdminRepository;
import com.library.repository.BookRepository;
import com.library.repository.BorrowRepository;
import com.library.repository.impl.AdminRepositoryImpl;
import com.library.repository.impl.BookRepositoryImpl;
import com.library.repository.impl.BorrowRepositoryImpl;
import com.library.service.BookService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BookServiceImpl implements BookService {

    //注入数据库操作
    private BookRepository bookRepository = new BookRepositoryImpl();
    //分页， 每页的数据数量
    private final Integer pageLimit = 6;
    private BorrowRepository borrowRepository = new BorrowRepositoryImpl();
    private AdminRepository adminRepository = new AdminRepositoryImpl();

    @Override
    public List<Book> findAll(int page) {
        Integer index = (page-1)*pageLimit;
        List<Book> list = bookRepository.findAll(index,pageLimit);
        return list;
    }

    /**
     * 获取 总页数
     * @return
     */
    @Override
    public int getPages() {
        int count = bookRepository.count();
        if (count % pageLimit == 0) {
            return count / pageLimit;
        } else {
            return count / pageLimit + 1;
        }
    }

    /**
     * 图书借阅操作，向数据库添加借阅记录信息
     * @param bookId
     * @param readerId
     */
    @Override
    public void addBorrow(Integer bookId, Integer readerId) {
        //借书时间
        Date jieShuData = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = simpleDateFormat.format(jieShuData);
        //还书事件是借书时间+30天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 30);
        Date huaiShuDate = calendar.getTime();
        String returnTime = simpleDateFormat.format(huaiShuDate);
        borrowRepository.Insert(bookId,readerId,borrowTime,returnTime,null,0);
    }

    /**
     * 通过读者id信息查询此读者所有的图书借阅信息
     *
     * @param id
     * @return
     */
    @Override
    public List<Borrow> findAllBorrowByReaderID(Integer id, Integer page) {
        //将page转换为index和limit
        Integer index = (page-1)*pageLimit;
        return borrowRepository.findAllByReaderID(id,index,pageLimit);
    }

    /**
     * 通过state查找所有已借阅未审核的图书借阅信息
     * @param state
     * @return
     */
    @Override
    public List<Borrow> findAllBorrowByState(Integer state, Integer page) {
        //将page转换为index和limit
        Integer index = (page-1)*pageLimit;
        return borrowRepository.findAllByState(state,index,pageLimit);
    }

    /**
     * 通过读者id信息，获取总页数
     * @param id
     * @return
     */
    @Override
    public int getBorrowPagesByReaderId(Integer id) {
        int count = borrowRepository.countByReaderId(id);
        if (count % pageLimit == 0) {
            return count / pageLimit;
        } else {
            return count / pageLimit + 1;
        }
    }

    /**
     * 统计所有相同状态的借阅信息
     * @return
     */
    @Override
    public int getBorrowPagesByState(Integer state) {
        int count = borrowRepository.countByState(state);
        if (count % pageLimit == 0) {
            return count / pageLimit;
        } else {
            return count / pageLimit + 1;
        }
    }

    /**
     * 处理借阅信息
     * @param borrowId
     * @param state
     * @param adminId
     */
    @Override
    public void handleBorrow(Integer borrowId, Integer state, Integer adminId) {
        adminRepository.handle(borrowId,state,adminId);
    }
}
